package com.ielts.activity;

import vn.com.baseapplication.constant.BaseConstant;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.ielts.IeltsConstant;
import com.ielts.services.GcmBackground;
import com.ielts.services.GcmIntentService;
import com.ielts.services.IeltsServices;

public class SplashActivity extends IeltsActivity{
	Handler mHandler = new Handler();
	Intent activityToStart;
	
	IeltsServices mServices;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		
		ImageView imv = new ImageView(this);
		imv.setImageResource(R.drawable.plash);
		imv.setScaleType(ScaleType.FIT_XY);
		setContentView(imv);
		
		String gcmId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_GCM_ID, IeltsConstant.KEY_GCM_ID_DEF);
		if(gcmId.equals(IeltsConstant.KEY_GCM_ID_DEF)){
			Log.e("DeviceToken", "device not exist. Try to get");
			GcmBackground.loadCnt = 0;
			new GcmBackground(IeltsApplication.getInstance()).execute();
			return;
		}
		else{
			Log.e("DeviceToken", "exist: "+gcmId);
			loadDone();
		}
		
		NotificationManager mNotificationManager = (NotificationManager)
				this.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancel(GcmIntentService.NOTIFICATION_ID);
	}
	
	public void loadDone(){
		String fbId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_FB_LINK_ID, IeltsConstant.KEY_FB_LINK_DEF);
		String userId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_ID, IeltsConstant.KEY_USER_ID_DEF);
		
		if(fbId.equals(IeltsConstant.KEY_FB_LINK_DEF)||userId.equals(IeltsConstant.KEY_USER_ID_DEF)){
			activityToStart = new Intent(SplashActivity.this, LoginActivity.class);
			startActivity(activityToStart);
			this.finish();
		}else{
			mHandler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					activityToStart = new Intent(SplashActivity.this, MainActivity.class);
					startActivity(activityToStart);
					SplashActivity.this.finish();
				}
			}, 800);
			
		}
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (!intent.hasExtra(BaseConstant.MSG_FLAG)) {
			return;
		}
		String FLAG = intent.getStringExtra(BaseConstant.MSG_FLAG) + "";
		if (FLAG != null && (FLAG + "").equals(IeltsConstant.LOAD_DONE+"")) {
			loadDone();
		}
	}
	
	@Override
	public void onServiceConnected() {
		super.onServiceConnected();
		mServices = (IeltsServices) getBaseServices();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (!isServiceConnected()) {
			bindBaseService(IeltsServices.class);
		} else {

		}
	}
}